/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package maincz.tests.webapp.dao;

import java.util.List;
import java.util.logging.Logger;

import maincz.tests.webapp.entity.contactbook.Contact;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Main
 */
@Repository
@Transactional
public class ContactDAO implements IContactDAO {

    @Autowired
    private SessionFactory sessionFactory;

    private static final Logger log = Logger.getLogger("MyLogger");

    public ContactDAO() {
        super();
        log.info("constructor ContactDAOImpl");
    }

    @Override
    public void addContact(Contact contact) {
        sessionFactory.getCurrentSession().save(contact);
    }

    @Override
    public List<Contact> listContacts() {
        return sessionFactory.getCurrentSession().createQuery("from Contact").list();
    }

    @Override
    public void removeContact(Long id) {
        Contact contact = (Contact) sessionFactory.getCurrentSession().load(Contact.class, id);
        if (contact != null) {
            sessionFactory.getCurrentSession().delete(contact);
        }
    }

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

}
